package com.easy.generator.java;

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @author easy
 * @date 2025年07月05日 19:36
 */
public class ExceptionGenerator {
    private static final String EXCEPTION_PACKAGE = Easy.MODEL_PACKAGE + ".exception";
    private static final String ADVICE_PACKAGE= Easy.MODEL_PACKAGE+".advice";
    private static final String EXCEPTION_NAME = Easy.SERVICE_EXCEPTION_NAME;
    private static final String ADVICE_NAME = "ServiceExceptionAdvice";
    private static final String EXCEPTION_TEMPLATE =
                    "package "+EXCEPTION_PACKAGE+";\n" +
                            "\n" +
                            "public class "+EXCEPTION_NAME+" extends RuntimeException{\n" +
                            "    private static final long serialVersionUID = 1L;\n" +
                            "    private int code=500;\n" +
                            "    public "+EXCEPTION_NAME+"(String message)\n" +
                            "    {\n" +
                            "        super(message);\n" +
                            "    }\n" +
                            "    public "+EXCEPTION_NAME+"(int code,String message)\n" +
                            "    {\n" +
                            "        super(message);\n" +
                            "        this.code=code;\n" +
                            "    }\n" +
                            "    public int getCode()\n" +
                            "    {\n" +
                            "        return code;\n" +
                            "    }\n" +
                            "\n" +
                            "}\n";
    public static void generateException() throws IOException {
        String content = String.format(EXCEPTION_TEMPLATE, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL+ Easy.SRC_DIR+ Easy.convertPackagePath(EXCEPTION_PACKAGE)+"/"+EXCEPTION_NAME+".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }
    private static final String ADVICE_TEMPLATE =
                    "package "+ADVICE_PACKAGE+";\n" +
                            "\n" +
                            "import com.easy.exception.ServiceException;\n" +
                            "import com.easy.util.Result;\n" +
                            "import org.springframework.web.bind.annotation.ExceptionHandler;\n" +
                            "import org.springframework.web.bind.annotation.RestControllerAdvice;\n" +
                            "\n" +
                            "@RestControllerAdvice\n" +
                            "public class "+ADVICE_NAME+" {\n" +
                            "\n" +
                            "    @ExceptionHandler("+EXCEPTION_NAME+".class)\n" +
                            "    public Result handle("+EXCEPTION_NAME+" e)\n" +
                            "    {\n" +
                            "        return Result.error(e.getCode(),e.getMessage());\n" +
                            "    }\n" +
                            "}\n" ;
    public static void generateAdvice() throws IOException {
        String content = String.format(ADVICE_TEMPLATE, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL+ Easy.SRC_DIR+ Easy.convertPackagePath(ADVICE_PACKAGE)+"/"+ADVICE_NAME+".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }
    public static void generate() throws IOException {
        Easy.initPackage(EXCEPTION_PACKAGE);
        Easy.initPackage(ADVICE_PACKAGE);
        generateException();
        generateAdvice();
    }

}
